﻿//-----------------------------------------------------------------------
// <copyright file="WebAuthenticationPage.xaml.cs" company="In The Hand Ltd">
//     Copyright © 2014 In The Hand Ltd. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using Microsoft.Phone.Controls;
using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace InTheHand.Security.Authentication.Web
{
    public partial class WebAuthenticationPage : PhoneApplicationPage
    {
        private string redirectUri;

        public WebAuthenticationPage()
        {
            InitializeComponent();

            Browser.Navigating += Browser_Navigating;
            Browser.Navigated += Browser_Navigated;
            Browser.NavigationFailed += Browser_NavigationFailed;
        }

        void Browser_Navigated(object sender, NavigationEventArgs e)
        {
            Progress.IsIndeterminate = false;
            Progress.Visibility = System.Windows.Visibility.Collapsed;
            Icon.Visibility = System.Windows.Visibility.Visible;
            AppName.Visibility = System.Windows.Visibility.Visible;
            Browser.Visibility = Visibility.Visible;

        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            Browser.Navigating -= this.Browser_Navigating;
            Browser.NavigationFailed -= this.Browser_NavigationFailed;

            base.OnNavigatingFrom(e);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Icon.Source = new BitmapImage(InTheHand.ApplicationModel.Package.Current.Logo);
            AppName.Text = InTheHand.ApplicationModel.Package.Current.DisplayName;

            redirectUri = NavigationContext.QueryString["returnUri"];
            Browser.Navigate(new Uri(this.NavigationContext.QueryString["uri"]));
            base.OnNavigatedTo(e);
        }        
        
        void Browser_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            Progress.IsIndeterminate = false;
            Progress.Visibility = System.Windows.Visibility.Collapsed;

            InTheHand.Security.Authentication.Web.WebAuthenticationBroker.Result = new WebAuthenticationResult() { ResponseStatus= WebAuthenticationStatus.ErrorHttp };
            
            if(e.Exception != null)
            {
                InTheHand.Security.Authentication.Web.WebAuthenticationBroker.Result.ResponseErrorDetail = (uint)e.Exception.HResult;
            }

            NavigationService.GoBack();
            InTheHand.Security.Authentication.Web.WebAuthenticationBroker.Handle.Set(); 
        }

        void Browser_Navigating(object sender, NavigatingEventArgs e)
        {
            if (e.Uri.OriginalString.Contains(redirectUri))
            {
                InTheHand.Security.Authentication.Web.WebAuthenticationBroker.Result = new WebAuthenticationResult();        
                InTheHand.Security.Authentication.Web.WebAuthenticationBroker.Result.ResponseData = e.Uri.OriginalString;

                if(e.Uri.OriginalString.Contains("error="))
                {
                    InTheHand.Security.Authentication.Web.WebAuthenticationBroker.Result.ResponseStatus = WebAuthenticationStatus.UserCancel;
                }

                NavigationService.GoBack();
                InTheHand.Security.Authentication.Web.WebAuthenticationBroker.Handle.Set(); 
            }
        }
    }
}